<%
  containerId = 'pie-chart-widget' + widget.id.to_s
  chartTitle = widget_properties["chartTitle"]
  filterId = widget_properties["filter"].to_i
  maxItems = widget_properties["maxItems"].to_i
  reverseOrder = widget_properties["reverseOrder"]
  relativeScale = widget_properties["relativeScale"]

  filter = MeasureFilter.find_by_id(filterId.to_i)
  if filter
    @widget_title = link_to h(filter.name), {:controller => 'measures', :action => 'filter', :id => filter.id, :display => 'list'}
%>

<div class="histogram-widget" id="<%= containerId %>">
  <!--[if lte IE 8 ]> <h3><%= message('widget.unsupported_browser_warning') -%></h3> <![endif]-->

  <!--[if (gte IE 9)|!(IE)]><!-->
  <% if chartTitle %>
  <h3><%= h(chartTitle) -%></h3>
  <% end %>
  <!--<![endif]-->
</div>

<!--[if (gte IE 9)|!(IE)]><!-->
<script>
  (function () {
    var metrics = [
          '<%= widget_properties["metric"].name -%>'
        ],
        query = [
          'filter=<%= filterId -%>',
          'metrics=' + metrics.join(','),
          'fields=name,longName,qualifier',
          'pageSize=<%= maxItems -%>',
          'page=1',
          'sort=metric:' + metrics[0],
          'asc=<%= reverseOrder -%>'
        ].join('&'),
        widget = new SonarWidgets.Widget();

    widget
      .type('Histogram')
      .source(baseUrl + '/measures/search_filter?' + query)
      .metricsPriority(metrics)
      .options({
        baseUrl: baseUrl + '/dashboard/index/',
        relativeScale: <%= relativeScale -%>,
        maxItemsReachedMessage: '<%= message("widget.measure_filter_histogram.max_items_reached", :params => [maxItems]) -%>',
        noData: '<%= message('no_data') -%>',
        noMainMetric: '<%= message('widget.measure_filter.no_main_metric') -%>'
      })
      .render('#<%= containerId -%>');

    autoResize(500, function() {
      widget.update('#<%= containerId -%>');
    });
  })();
</script>
<!--<![endif]-->
<% else %>
  <p><%= image_tag 'warning.png' %> <%= message 'measure_filter.widget.unknown_filter_warning' -%></p>
<% end %>
